package com.zrsx.managemodel.service.serviceImpl;

import com.zrsx.managemodel.common.dto.RecordDTO;
import com.zrsx.managemodel.common.entity.Record;
import com.zrsx.managemodel.common.util.SnowflakeIdGenerator;
import com.zrsx.managemodel.mapper.RecordMapper;
import com.zrsx.managemodel.service.RecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private OpenFeignService openFeignService;

    @Override
    public void addRecord(RecordDTO recordDTO) {
        //如果更新基础信息，则非加减，而是直接更新
        if(recordDTO.getRecordType().equals("基本信息")){
            recordDTO.setChangeCredit(recordDTO.getChangeCredit()
                    - openFeignService.getCreditDimension(recordDTO.getUserId(),recordDTO.getRecordType()).getData());
        }

        Record record = new Record();
        BeanUtils.copyProperties(recordDTO, record);
        //雪花获取id，数据中心id+机器id
        SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
        record.setRecordId(idGenerator.nextId());
        System.out.println("Mapper层，record="+record);
        recordMapper.insertRecord(record);
    }
}
